package com.mayisoftware.mayioa.business.plan.impl.serviceImpl;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanAchievement;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanDetailConfig;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanArchService;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConfigDetailService;
import com.mayisoftware.mayioa.business.plan.impl.dao.PlanArchMapper;
import com.mayisoftware.mayioa.common.impl.support.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlanAchServiceImpl implements IPlanArchService {

    @Autowired
    private PlanArchMapper planArchMapper;

    @Autowired
    private IPlanConfigDetailService planDetailService;

    /**
     * 根据条件分页查询绩效
     *
     * @param planAchievement 绩效
     * @return 绩效列表
     */
    @Override
    public List<PlanAchievement> selectPlanAchList(PlanAchievement planAchievement) {
        return planArchMapper.selectPlanAchList(planAchievement);
    }

    /**
     * 查询不在当前计划中的绩效
     * @param planCfgId 计划ID
     * @return 绩效列表
     */
    public List<PlanAchievement> selectPlanAchList(Long planCfgId)
    {
        return planArchMapper.selectPlanAchListByPlanId(planCfgId);
    }

    /**
     * 通过绩效ID查询绩效信息
     *
     * @param planAchId 绩效ID
     * @return
     */
    @Override
    public PlanAchievement selectPlanAchById(Long planAchId) {
        return planArchMapper.selectPlanAchById(planAchId);
    }

    /**
     * 插入新计划绩效
     *
     * @param planAchievement 绩效
     * @return 结果
     */
    @Override
    public int insertPlanAch(PlanAchievement planAchievement) {
        return planArchMapper.insertPlanAch(planAchievement);
    }

    /**
     * 修改计划绩效信息
     *
     * @param planAchievement 计划绩效
     * @return
     */
    @Override
    public int updatePlanAch(PlanAchievement planAchievement) {
        planDetailService.updatePlanItemName(planAchievement.getPlanAchId(), planAchievement.getPlanAchName());
        return planArchMapper.updatePlanAch(planAchievement);
    }

    /**
     * 批量删除计划绩效信息
     * @param ids 绩效ID：用,分割
     * @return
     */
    @Override
    public int deletePlanAchByIds(String ids) {
        Long[] idArray = Convert.toLongArray(ids);
        List<Long> removeIds = new ArrayList<Long>();
        for(Long id : idArray){
            List<PlanDetailConfig> inPlan = planDetailService.selectPlanItemByCfgItemIdAndCfgItemType(3,id.intValue());
            //查询绩效id不在计划中
            if (inPlan.isEmpty()){
                removeIds.add(id);
            }
        }
        return planArchMapper.deletePlanAchByIds((Long[]) removeIds.toArray());
    }

    /**
     * 查询绩效最大分数
     * @return
     */
    public Integer selectPlanAchMaxScore()
    {
        return planArchMapper.selectPlanAchMaxScore();
    }
}
